package com.week2.day05;

/**
 * 替换字符串的叠字
 * 例如，对于字符串 "hello nice to meet you"
 * 将其中出现两次的字符 ll 和 ee 分别替换为 l~ 和 e~
 * 输出为：hel~o nice to me~t you
 * "浩浩荡荡"
 * 输出为： 浩~荡~
 */
public class Homework02 {

    public static void main(String[] args) {
        String s = "hello nice to meet you";
        String r = dealStr(s);
        System.out.println(r);
    }

    public static String dealStr(String s) {
        String s1 = "";
        String s2 = "";
        for (int i = 0; i < s.length() - 1; i++) {
            if (s.charAt(i) == s.charAt(i + 1)) {
                s1 = s.substring(0, i + 1);
                s2 = s.substring(i + 2);
                s = String.format(s1 + "%s" + s2, "~");
            }
        }
        return s;
    }

}
